<%

// Index pages are no longer used.
// Remove this file when there are no more {{index}} calls in translated-content
// See also https://github.com/mdn/content/pull/13770

// Throw a MacroDeprecatedError flaw
mdn.deprecated()

// Builds an index / inventory table for a section
//
// Parameters:
//  $0  -  The slug to build the index for (e.g. "/en-us/docs/Web/JavaScript")


var l10nHeading = mdn.localString({
    "en-US": "Found #nbP# pages:",
    "fr"   : "#nbP# pages trouvées\xa0:",
    "ja"   : "#nbP# ページあります:",
    "ru"   : "Найдено #nbP# страниц:",
    "pt-BR": "Encontradas #nbP# páginas:"
});

var l10nNumber = mdn.localString({
    "en-US": "#",
    "fr"   : "N°",
    "ja"   : "#",
    "ru"   : "№",
    "pt-BR": "Nº"
});

var l10nPage = mdn.localString({
    "en-US": "Page",
    "fr"   : "Page",
    "ja"   : "ページ",
    "ru"   : "Страница",
    "pt-BR": "Páginas"
});

var l10nTags = mdn.localString({
    "en-US": "Tags and summary",
    "fr"   : "Étiquettes et résumé",
    "ja"   : "タグと要約",
    "ru"   : "Теги и описание",
    "pt-BR": "Tags e Resumo"
});


var section = $0;

var pageList = await page.subpagesExpand(section, '', true);
var result = [];
var urlsSeen = new Set();

function getPages(pageList) {
    if (pageList) {
        for (var i = 0; i < pageList.length; i++) {
            var page = pageList[i];
            if (urlsSeen.has(page.url)) {
                continue;
            }
            // Never include a page that is called "Index" because it's usually
            // pointless since it's clearly the page you're already on.
            // See https://github.com/mdn/yari/issues/1468
            if (section + '/Index' === page.url) {
                continue;
            }
            urlsSeen.add(page.url);
            result.push({
                title: page.title.replace(/</g,'&lt;').replace(/>/g,'&gt;'),
                url: page.url,
                tags: [...page.tags].sort(),
                summary: page.summary || '<i>No summary!</i>'
            })
            getPages(page.subpages);
        }
    }
}

getPages(pageList);

%>

<p><strong><%=l10nHeading.replace("#nbP#",result.length)%></strong></p>
<table class="fullwidth-table standard-table">
<thead>
    <th><%=l10nNumber%></th>
    <th><%=l10nPage%></th>
    <th><%=l10nTags%></th>
</thead>
<tbody>

<% for (i=0; i < result.length; i++) { %>
<tr>
    <td rowspan="2"><%=i+1%></td>
    <td rowspan="2"><a href='<%=result[i].url%>'><%-result[i].title%></a></td>
    <td><strong><%=result[i].tags.join(", ")%></strong></td>
</tr>
<tr>
    <td><%-result[i].summary%></td>
<tr>
<% } %>

</tbody>
</table>
